Page({
    data : {
      params: {
        page: 1,
        size: 999999,
        userId: wx.getStorageSync('userId'),
        bookInfoId: ''
      },
      btnStatus: '',
      pic: '',
      name: '',
      price: '',
      author: '',
      maxshubi: '',

      list: []
    },

    //添加读后感
  addBookfeel () {
    wx.navigateTo({
      url: '/package/my/pages/addBookfeel/addBookfeel?bookids=' + this.data.params.bookInfoId + '&pic=' + this.data.pic + '&name=' + this.data.name + '&price=' + this.data.price + '&author=' + this.data.author,
    })
  },



  //获取列表

  getList (params) {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxBookTransRecord/find/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code) {


          for (var i = 0;i< res.data.data.length;i++) {
            res.data.data[i].loadMore = true
          }
          // var arr = []

          // res.data.data.forEach(function (item, index) {
          //   arr.push(item)

          // })

          _this.setData({
            list: res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  onLoad (options) {
    console.log(options.bookid)
    // var bookid = options.bookid
    var bookids = "params.bookInfoId"
    this.setData({
      [bookids]: options.bookid,
      name: options.name,
      pic: options.pic,
      price: options.price,
      author: options.author,
      maxshubi: options.maxshubi
    })
  },

  //点赞

  dianzan (e) {
    console.log(e)

    var feelid = e.currentTarget.dataset.feelid
    var option = e.currentTarget.dataset.option
    this.dianzanApi(feelid, option)
  },

  dianzanApi (feelid, option) {
    var _this = this
    var params = {
      bookAfterReadingId: feelid,
      option: option
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/afterReading/thumb/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          if (option==1) {
            for (var i = 0;i< _this.data.list.length;i++) {
              if (feelid == _this.data.list[i].bookAfterReadingNoteId) {
                var thumbType = "list[" + i + "].thumbType"
                var thumbUpNum = "list[" + i + "].thumbUpNum"
                var thumbDownNum = "list[" + i + "].thumbDownNum"
                if (_this.data.list[i].thumbType=='') {
                  _this.setData({
                    [thumbType]: "1",
                    [thumbUpNum]: _this.data.list[i].thumbUpNum +=1,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum
                  })
                  return
                }
                if (_this.data.list[i].thumbType==1) {
                  _this.setData({
                    [thumbType]: '',
                    [thumbUpNum]: _this.data.list[i].thumbUpNum -= 1,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum
                  })
                  return
                }
                if (_this.data.list[i].thumbType == 2) {
                  _this.setData({
                    [thumbType]: "1",
                    [thumbUpNum]: _this.data.list[i].thumbUpNum += 1,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum-=1
                  })
                  return
                }
              }
            }
      }
      //点赞结束
          //点踩
          if (option == 2) {
            // debugger
            for (var i = 0; i < _this.data.list.length; i++) {
              if (feelid == _this.data.list[i].bookAfterReadingNoteId) {
                var thumbType = "list[" + i + "].thumbType"
                var thumbUpNum = "list[" + i + "].thumbUpNum"
                var thumbDownNum = "list[" + i + "].thumbDownNum"
                if (_this.data.list[i].thumbType == '') {
                  _this.setData({
                    [thumbType]: "2",
                    [thumbUpNum]: _this.data.list[i].thumbUpNum,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum += 1
                  })
                  return
                }
                if (_this.data.list[i].thumbType == 1) {
                  _this.setData({
                    [thumbType]: "2",
                    [thumbUpNum]: _this.data.list[i].thumbUpNum -= 1,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum += 1
                  })
                  return
                }
                if (_this.data.list[i].thumbType == 2) {
                  _this.setData({
                    [thumbType]: "",
                    [thumbUpNum]: _this.data.list[i].thumbUpNum,
                    [thumbDownNum]: _this.data.list[i].thumbDownNum -= 1
                  })
                  return
                }
              }
            }
          }
          //点踩结束
        }

      }
    })
  },


  goMore (e) {
    console.log(e)

    var index = e.currentTarget.dataset.index

    var loadMore = "list[" + index + "].loadMore"

    this.setData({
      [loadMore]: false
    })
  },

  goMore2 (e) {
    var index = e.currentTarget.dataset.index

    var loadMore = "list[" + index + "].loadMore"

    this.setData({
      [loadMore]: true
    })
  },




  onShareAppMessage(res) {

    var _this = this
    if (res.from == 'button') {
      console.log(res.target, res)
    }

    return {
      title: _this.data.name,
      path: '/package/index/pages/bookdetail/bookdetail?bookId=' + _this.data.params.bookInfoId,
      imageUrl: _this.data.pic
    }
  },


  getReadings () {
    var _this = this
    var params = {
      bookInfoId: this.data.params.bookInfoId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/afterReading/judgeInsertAfterReading/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            btnStatus: res.data.data ? true: false
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  onShow () {
    this.getReadings()
    this.getList(this.data.params)
  }
})
